﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using SmartPaginator.Elements;
using System.Windows.Documents;

namespace SmartPaginator
{
    public class HarvestingContext
    {
        #region Constructor

        public HarvestingContext()
        {
            HarvestedElements = new List<DependencyObject>();
            CurrentPage = 1;
            CurrentSectionGroup = 1;
            SectionGroupPageCount = new Dictionary<int, int>();
            CurrentSection = null;
        }

        #endregion

        #region Properties

        protected List<DependencyObject> HarvestedElements { get; private set; }

        public int CurrentPage { get; set; }

        public int CurrentSectionGroup { get; set; }

        public Dictionary<int, int> SectionGroupPageCount { get; private set; }

        public Section CurrentSection { get; set;} 

        #endregion

        #region Methods

        public void MarkAsFullyHarvested(DependencyObject element)
        {
            if (!HarvestedElements.Contains(element))
                HarvestedElements.Add(element);
        }

        public bool IsFullyHarvested(DependencyObject element)
        {
            return HarvestedElements.Contains(element);
        }

        public bool IsPartiallyHarvested(DependencyObject element)
        {
            if (IsFullyHarvested(element))
                return false;

            foreach (DependencyObject child in LogicalTreeHelper.GetChildren(element))
            {
                if (IsFullyHarvested(child))
                    return true;

                if (IsPartiallyHarvested(child))
                    return true;
            }
            return false;
        }

        public bool HasStartedHarvesting(DependencyObject element)
        {
            if (IsFullyHarvested(element))
                return true;

            return IsPartiallyHarvested(element);
        }

        #endregion
    }
}
